package org.example.market.service.providerService;

import org.example.market.entity.Provider;
import org.example.market.mapper.ProviderMapper;
import org.example.market.util.PageResult;
import org.example.market.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProviderService {

    @Autowired
    private ProviderMapper providerMapper;

    public Result<Provider> addProvider(Provider provider) {
        try {
            providerMapper.insertProvider(provider);
            return Result.success(provider);
        } catch (Exception e) {
            return Result.error(500, "添加供应商失败: " + e.getMessage());
        }
    }

    public Result<List<Provider>> getAllProviderList() {
        try {
            List<Provider> providers = providerMapper.getAllProviders();
            return Result.success(providers);
        } catch (Exception e) {
            return Result.error(500, "获取供应商列表失败: " + e.getMessage());
        }
    }

    public Result<Provider> getProviderDetail(int id) {
        try {
            Provider provider = providerMapper.getProviderById(id);
            if (provider != null) {
                return Result.success(provider);
            }
            return Result.error(404, "未找到该供应商");
        } catch (Exception e) {
            return Result.error(500, "获取供应商详情失败: " + e.getMessage());
        }
    }

    public Result<Provider> updateProvider(Provider provider) {
        try {
            providerMapper.updateProvider(provider);
            return Result.success(provider);
        } catch (Exception e) {
            return Result.error(500, "更新供应商失败: " + e.getMessage());
        }
    }

    public Result<Void> deleteProvider(Long id) {
        try {
            providerMapper.deleteProviderById(Math.toIntExact(id));
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "删除供应商失败: " + e.getMessage());
        }
    }

//    public Result<PageResult<Provider>> findByPage(int currentPage, int pageSize, String proCode, String proName) {
//        try {
//            List<Provider> providers = providerMapper.findByPage(currentPage, pageSize, proCode, proName);
//            int total = providerMapper.countByCondition(proCode, proName);
//
//            PageResult<Provider> pageResult = new PageResult<>();
//            pageResult.setList(providers);
//            pageResult.setTotal(total);
//            pageResult.setCurrentPage(currentPage);
//            pageResult.setPageSize(pageSize);
//
//            return Result.success(pageResult);
//        } catch (Exception e) {
//            return Result.error(500, "分页查询供应商失败: " + e.getMessage());
//        }
//    }


    public Result<PageResult<Provider>> findByPage(int currentPage, int pageSize, String proCode, String proName) {
        try {
            // 计算正确的起始索引：(当前页-1)*每页条数
            int startIndex = (currentPage - 1) * pageSize;

            // 传入起始索引和每页条数，而非当前页码
            List<Provider> providers = providerMapper.findByPage(startIndex, pageSize, proCode, proName);
            int total = providerMapper.countByCondition(proCode, proName);

            PageResult<Provider> pageResult = new PageResult<>();
            pageResult.setList(providers);
            pageResult.setTotal(total);
            pageResult.setCurrentPage(currentPage);
            pageResult.setPageSize(pageSize);

            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error(500, "分页查询供应商失败: " + e.getMessage());
        }
    }
}